package com.love_weinuo.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.love_weinuo.R;
import com.love_weinuo.bean.MyOrderChangeCallbackBean;
import com.love_weinuo.databinding.ItemIndexFirstBinding;
import com.love_weinuo.ui.view.RxProgressBar;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2019/3/18.
 */

public class IndexFirstAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;

    public IndexFirstAdapter(Context context, List<MyOrderChangeCallbackBean.ResultEntity.ResEntity> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemIndexFirstBinding functionBinding = DataBindingUtil.inflate(LayoutInflater.from(mcontext), R.layout.item_index_first, parent, false);
        functionBinding.setAdapter(this);
        return new TourViewHolder(functionBinding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;
        tv.tvModuleStartTime.setText(strings.get(position).getStarttime().split(" ")[0]);
        tv.tvModuleEndTime.setText(strings.get(position).getEndtime().split(" ")[0]);
        if ((strings.get(position).getSumarea() - strings.get(position).getArea()) > 0) {
            tv.tvModuleResidue.setText((strings.get(position).getSumarea() - strings.get(position).getArea()) + "");
            BigDecimal b = new BigDecimal((float)strings.get(position).getArea() / strings.get(position).getSumarea());
            double result = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
            tv.bar.setProgress((float)result*100);
        }else{
            tv.tvModuleResidue.setText("0");
            tv.bar.setProgress(100);
        }
        tv.tvModuleArea.setText(strings.get(position).getArea() + "");
        /**
         * 0 待拼单 1待付定金 2 代付付款 3  待作业 4 已完成
         */
        switch (strings.get(position).getOrderstate()) {
            case "0":
                tv.tvModuleStatue.setText("待拼单");
                break;
            case "3":
                tv.tvModuleStatue.setText("待作业");
                break;
            case "2":
                tv.tvModuleStatue.setText("代付尾款");
                break;
            case "4":
                tv.tvModuleStatue.setText("已完成");
                break;
            default:
        }

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<MyOrderChangeCallbackBean.ResultEntity.ResEntity> {
        ItemIndexFirstBinding functionBinding;
        RxProgressBar bar;
        TextView tvModulePindan, tvModuleResidue, tvModuleArea, tvModuleEndTime, tvModuleStartTime, tvModuleStatue;

        public TourViewHolder(ItemIndexFirstBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            bar = dataBinding.rpModuleProgressBar;
            tvModulePindan = dataBinding.tvModulePindan;
            tvModuleResidue = dataBinding.tvModuleResidue;
            tvModuleArea = dataBinding.tvModuleArea;
            tvModuleStartTime = dataBinding.tvModuleStartTime;
            tvModuleEndTime = dataBinding.tvModuleEndTime;
            tvModuleStatue = dataBinding.tvModuleStatue;
        }

        @Override
        public void bindData(MyOrderChangeCallbackBean.ResultEntity.ResEntity data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

}